#include "GameOverLayer.h"

bool GameOverLayer::init() {
	if (!Layer::init()) return false;

	AUDIOENGINE->preloadBackgroundMusic(GAMEOVER_BGM_NAME);
	auto rootNode = CSLoader::createNode(GAMEOVERLAYER_CSB_NAME);
	this->addChild(rootNode);
	auto action = CSLoader::createTimeline(GAMEOVERLAYER_CSB_NAME);
	rootNode->runAction(action);
	action->gotoFrameAndPlay(0, false);

	if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY)) {
		AUDIOENGINE->stopAllEffects();
		AUDIOENGINE->stopBackgroundMusic();
		AUDIOENGINE->playEffect(GAMEOVER_BGM_NAME);
	}

	auto returnButton = rootNode->getChildByName<Button *>(RETURNBUTTON);
	returnButton->addClickEventListener([=](Ref *ref) {
		DIRECTOR->replaceScene(MenuScene::createScene());
	});

	return true;
}